﻿@using CodeGenerator.Models.TemplateModels
@inherits RazorEngine.Templating.TemplateBase<CodeGenerator.Models.TemplateModels.ServiceFileModel>
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using @(Model.EntityNamespace);
using Enterprise.Application.@(Model.DirName).Dto;
using Enterprise.Dtos;

namespace Enterprise.Application.@(Model.DirName)
{
    /// <summary>
    /// @(Model.Name)应用层服务的接口实现方法
    ///</summary>
    public class @(Model.Name)AppService : EnterpriseAppServiceBase, I@(Model.Name)AppService
    {
        private readonly IRepository<@(Model.Name), Guid> _entityRepository;

        //private readonly I@(Model.Name)Manager _entityManager;

        /// <summary>
        /// 构造函数
        ///</summary>
        public @(Model.Name)AppService(
        IRepository<@(Model.Name), Guid> entityRepository 
    //,I@(Model.Name)Manager entityManager 
    )
        {
            _entityRepository = entityRepository;
          //  _entityManager=entityManager;
        }


        /// <summary>
        /// 获取@(Model.Name)的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageReturns<@(Model.Name)ListDto>> GetPaged(Get@(Model.Name)sInput input)
		{

		    var query = _entityRepository.GetAll();
            // TODO:根据传入的参数添加过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                //query = query.Where(r => r.Title.Contains(input.Filter));
            }
     
			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			var entityListDtos =entityList.MapTo<List<@(Model.Name)ListDto>>();

			return new PageReturns<@(Model.Name)ListDto>(count,entityListDtos);
		}



		/// <summary>
		/// 添加或者修改@(Model.Name)的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>

		public async Task CreateOrUpdate(CreateOrUpdate@(Model.Name)Input input)
		{

			if (input.@(Model.Name).Id.HasValue)
			{
				await Update(input.@(Model.Name));
			}
			else
			{
				await Create(input.@(Model.Name));
			}
		}


		/// <summary>
		/// 新增@(Model.Name)
		/// </summary>

		protected virtual async Task<@(Model.Name)EditDto> Create(@(Model.Name)EditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <@(Model.Name)>(input);
            var entity=input.MapTo<@(Model.Name)>();

            entity.CreateTime = DateTime.Now;
			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<@(Model.Name)EditDto>();
		}

		/// <summary>
		/// 编辑@(Model.Name)
		/// </summary>

		protected virtual async Task Update(@(Model.Name)EditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}


		/// <summary>
		/// 批量删除@(Model.Name)的方法
		/// </summary>

		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}

    }
}




